package com.dsit.sq.mydefineview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.dsit.sq.R;

/**
 * Created by dsit_dev04 on 2016/10/21.
 */
public class LongColorfulProgressBar extends View {
    public int totalRetangleCounts = 34;
    public int eachWidth = 3;
    private int width, height;
    public float intervalWidth = (float) 2.7;
    private Bitmap moveBitmap;
    Paint mPaint;
    public int progress = 0;
    public boolean isAllRed = false;

    public LongColorfulProgressBar(Context context) {
        super(context);
        init();
    }

    public LongColorfulProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LongColorfulProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        moveBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_jiantou);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(getResources().getColor(R.color.color_gray));
        mPaint.setStyle(Paint.Style.FILL);
    }

    private int[] mColors = new int[]{0xFFd50a0d, 0xFFd5320d, 0xFFd55a0d,
            0xFFd5820d, 0xFFd5a90d, 0xFFd5bc0d, 0xFFd3d60d, 0xFFa7d60d};

    private int getColor(int i) {
        if (i < 4) {
            return mColors[i];
        } else if (Math.abs(i - 33) < 4) {
            return mColors[Math.abs(i - 33)];
        } else if ((4 <= i && i <= 9) || (4 <= Math.abs(i - 33) && Math.abs(i - 33) <= 9)) {
            return mColors[4];
        } else if (i == 10 || i == 23) {
            return mColors[5];
        } else if (i == 11 || i == 22) {
            return mColors[6];
        } else {
            return mColors[7];
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //每个圆角矩形条宽度    总宽度-两边间隔-中间间隔的总宽度再除以总条数，就是每条的长度
        int imageWidth = 41;
        int imageHeight = 39;
        eachWidth = (int) ((width - 2 * (Dp2Px(getContext(), 4)) - (totalRetangleCounts - 1) * Dp2Px(getContext(), (float) 2.5)) / totalRetangleCounts);
        RectF f;
        RectF moveRect;
        int count = progress * totalRetangleCounts / 100;
        for (int i = 0; i < totalRetangleCounts; i++) {
            float left = i * ((Dp2Px(getContext(), intervalWidth)) + eachWidth) + (Dp2Px(getContext(), 4));
            float top = 45;
            float right = i * (Dp2Px(getContext(), intervalWidth)) + (Dp2Px(getContext(), 4)) + (i + 1) * eachWidth;
            float bottom = height;
            f = new RectF(left, top, right, bottom);
            if (isAllRed) {
                mPaint.setColor(getColor(0));
            } else {
                if (i < count) {
                    mPaint.setColor(getColor(i));
                } else {
                    mPaint.setColor(getResources().getColor(R.color.color_gray));
                }
            }
            //画每个圆角矩形
            if (totalRetangleCounts == 34 ){
                canvas.drawRoundRect(f, eachWidth / 2, eachWidth / 2, mPaint);
            }else if(totalRetangleCounts == 16){
                canvas.drawRoundRect(f, eachWidth/8, eachWidth/8, mPaint);
            }

        }
        if (count == 0) {
            count = 1;
        }
        int cx1 = 0;
        int cx2 = 0;
        if (totalRetangleCounts == 34 ){
            cx1 = 6;
            cx2 = 6;
        }else if(totalRetangleCounts == 16){
            cx1 = 2;
            cx2 = 30;
        }
        float left1 = (count - 1) * ((Dp2Px(getContext(), intervalWidth)) + eachWidth) + (Dp2Px(getContext(), 4)) + (eachWidth - imageWidth) / 2 - cx1;
        float top1 = 0;
        float right1 = (count - 1) * (Dp2Px(getContext(), intervalWidth)) + (Dp2Px(getContext(), 4)) + count * eachWidth + (eachWidth + imageWidth) / 2 - cx2;
        float bottom1 = imageHeight;
        moveRect = new RectF(left1, top1, right1, bottom1);
        canvas.drawBitmap(moveBitmap, null, moveRect, null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        //getsize();
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    //Dp
    public float Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (dp * scale + 0.5f);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setIsAllRed(boolean isAllRed){
        this.isAllRed = isAllRed;
    }

}
